var objectLink = new Array();
var objectLinkCheck = new Array();
var currentSubPage = {};
function UniqueArray(data)
{
    var tmp = {}, out = [];
    for(var i = 0, n = data.length; i < n; ++i)
    {
    	if(!tmp[data[i].subId]) { 
			if(data[i].subId==0){
				tmp[data[i].subId] = false;
			}else{
				tmp[data[i].subId] = true;
			}
			out.push(data[i]); 
		}
    }
    return out;
}

function gotoaddCategory(dom,dataSet,titleAddPospoint){
	currentSubPage.posId = dataSet.posId;
	if(titleAddPospoint!=undefined){
		var dialog = editAdminDataDialog("left,top",dom,label_addCate,"/adminconsole/POSManager/POSCategories/AddPOSCategories.jsp"
				,"addDialogCategory",AddEventClickCatePOSPoint,loadElementFinishCategory,dataSet);
	}else{
		var dialog = editAdminDataDialog("left,top",dom,label_addCate,"/adminconsole/POSManager/POSCategories/AddPOSCategories.jsp"
				,"addDialogCategory",AddEventClickCate,loadElementFinishCategory,null);
	}
}

function gotoaddSub(dom,data){
	 var dialog = editAdminDataDialog("left,top",dom,label_addSub,"/adminconsole/POSManager/POSSubcategories/AddPOSSubCategories.jsp"
			 ,"addDialogSub"+data.posId,AddEventClickCateSub,loadTaxElementFinishSubCategory,data);
}
function gotoaddSubCatePage(dom,data){
	 var dialog = editAdminDataDialog("left,top",dom,label_addSub,"/adminconsole/POSManager/POSSubcategories/AddPOSSubCategories.jsp"
			 ,"addDialogSub"+data.posId,AddEventClickCateSubCatePage,loadTaxElementFinishSubCategory,data);
}

function AddEventClickCatePOSPoint(dialog,dataSet){
	var title = dialog.find("#title").val();
	var pospoint = dialog.find("[name=pospoint]").val();
	var des = dialog.find("#description").val();
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	taxuse = taxuse.substring(0, taxuse.length-1);
	var url = "/POSCategoriesService";
	if(dataSet == null){
		var func = "addCategories";
	}else{
		if(dataSet.idcategory!="")
			var func = "addCategories";
		else
			var func = "editCategories";
	}
	var id = null;
	if(dataSet!=null){
		id = dataSet.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			pospoint : pospoint,
			taxuse:taxuse,
			description : des
		}
	simpleserviceWithArgs(url,data,showPOS,'add');
}

function AddEventClickCateSubCatePage(dialog,dataSet){
	var title = dialog.find("#title").val();
	var pospoint = dialog.find("[name=pospoint]").val();
	var des = dialog.find("#description").val();
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	
	taxuse = taxuse.substring(0, taxuse.length-1);
	var url = "/POSCategoriesService";
	var func = "addSubCategories";
	var id = null;
	if(dataSet!=null){
		id = dataSet.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			idcategory:dataSet.idcategory,
			pospoint : pospoint,
			taxuse:taxuse,
			description : des
		}
	simpleserviceWithArgs(url,data,showPOSCategory,'addsubCatePage');
}

function AddEventClickCateSub(dialog,dataSet){
	var title = dialog.find("#title").val();
	var pospoint = dialog.find("[name=pospoint]").val();
	var des = dialog.find("#description").val();
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	taxuse = taxuse.substring(0, taxuse.length-1);
	var url = "/POSCategoriesService";
	var func = "addSubCategories";
	var id = null;
	if(dataSet!=null){
		id = dataSet.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			idcategory:dataSet.idcategory,
			pospoint : pospoint,
			taxuse:taxuse,
			description : des
		}
	simpleserviceWithArgs(url,data,showPOSCategory,'addsub');
}

function AddEventClickCate(dialog,dataSet){
	var title = dialog.find("#title").val();
	var pospoint = dialog.find("[name=pospoint]").val();
	var des = dialog.find("#description").val();
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	taxuse = taxuse.substring(0, taxuse.length-1);
	var url = "/POSCategoriesService";
	if(dataSet == null){
		var func = "addCategories";
	}else{
		var func = "editCategories"
	}
	var id = null;
	if(dataSet!=null){
		id = dataSet.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			pospoint : pospoint,
			taxuse:taxuse,
			description : des
		}
	simpleserviceWithArgs(url,data,showPOSCategory,'add');
}

function loadElementFinishCategory(dialog,data){
	dialog.parents(".ui-dialog:first").css("width",600);
	setOptionPosPoint(data);
}

function loadTaxElementFinishSubCategory(dialog,data){
	dialog.parents(".ui-dialog:first").css("width",600);
	setOptionPosPoint(data);
}

function EditPOSUnit(dom,dataSet){
	var dialog = editAdminDataDialog("right",dom,label_addCate,"/adminconsole/POSManager/POSCategories/AddPOSCategories.jsp",
			"addDialogCategory",AddEventClickCate,loadEditElementFinishCate,dataSet);
}
function EditPOSUnitSub(dom,dataSet){
	var dialog = editAdminDataDialog("right",dom,label_addCate,"/adminconsole/POSManager/POSCategories/AddPOSCategories.jsp",
			"addDialogCategory",EditEventClickCate,loadEditElementFinishCate,dataSet);
}

function EditEventClickCate(dialog,dataSet){
	var title = dialog.find("#title").val();
	var pospoint = dialog.find("[name=pospoint]").val();
	var des = dialog.find("#description").val();
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	taxuse = taxuse.substring(0, taxuse.length-1);
	var url = "/POSCategoriesService";
	var func = "editCategories"
	var id = null;
	if(dataSet!=null){
		id = dataSet.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			pospoint : pospoint,
			taxuse:taxuse,
			description : des
		}
	simpleserviceWithArgs(url,data,showPOSCategory,'editsub');
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
}

function loadEditElementFinishCate(dialog,dataSet){
	cursorDialogResize(dialog,{width:600});
	setOptionPosPoint(dataSet.taxId);
	EditCategories(dataSet.id,dialog);
}

function setOptionPosPoint(data2){
	var url = "/POSCategoriesService";
	var data = { func : "getPOS"}
	simpleserviceWithArgs(url,data,setSuccess,data2);
}
function setSuccess(data,data2){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("[name=pospoint] option").remove();
	for (key in objArray[0]) {
		$("[name=pospoint]").append('<option value="' + objArray[0][key].id + '">' + objArray[0][key].Title + '</option>');
	}
	if(data2!=null){
		if(data2.idcategory==undefined||data2.idcategory==""){
			var data2split = data2.split(",");
			for(var i =0;i<data2split.length;i++){
				for(var j =0;j<$("[name=checktaxCate]:visible").length;j++){
					if($($("[name=checktaxCate]:visible")[j]).attr("id")==data2split[i])
						$($("[name=checktaxCate]:visible")[j]).attr('checked', true);
				}
			}
		}
		if($("#textposid").val()!=undefined||data2!=undefined){
			var dataset = "";
			if(data2==undefined){
				dataset = $("#textposid").val();
			}else{
				dataset = data2.posId;
				$("[name=idCategories]").val(data2.idcategory);
			}
			if(data2.idcategory!="pospoint"){
				$("[name=pospoint] option[value=" +dataset+"]").attr("selected","selected");
				$("[name=pospoint]").attr("disabled","disabled");
			}else{
				$("[name=pospoint]:visible option[value=" +data2.posId+"]").attr("selected","selected");
			}
			dataset = "";
		}	
	}else{
		$("[name=pospoint]:visible option[value=" +currentSubPage.posId+"]").attr("selected","selected");
	}
}

function setcurrentSubPage(id,addTitle,categoryId,posId,sub,subId){
	if((id!=undefined&&id!=undefined&&id!="")&&(id!="next"&&id!="previous"))
		currentSubPage.id = id;
	if(addTitle!=undefined&&addTitle!=undefined&&addTitle!="")
		currentSubPage.addTitle = addTitle;
	if(categoryId!=undefined&&categoryId!=undefined&&categoryId!="")
		currentSubPage.categoryId = categoryId;
	if(posId!=undefined&&posId!=undefined&&posId!="")
		currentSubPage.posId = posId;
	if(sub!=undefined&&sub!=undefined&&sub!="")
		currentSubPage.sub = sub;
	if(subId!=undefined&&subId!=undefined&&subId!="")
		currentSubPage.subId = subId;
	if(addTitle!=undefined&&posId!=undefined&&categoryId==""){
		currentSubPage.pointPos = addTitle;
		currentSubPage.pointPosId = posId;
	}
}

function showPOSCategory(data,id,addTitle,categoryId,posId,sub,subId,main,type){
	var objArray = new Array();
	if(data!=null&&data!='add'&&data!="next"&&data!=""){
		objArray = JSON.parse(data);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if($(addTitle).parent().length!=0){
			if($(addTitle).parent().parent().find("[name=titleName]").html()==undefined)
				addTitle = $(addTitle).html();
			else if($(addTitle).parent().parent().find("#subTitle").html()!=undefined)
				addTitle = $(addTitle).parent().parent().find("#subTitle").html();
			else
				addTitle = $(addTitle).parent().parent().find("[name=titleName]").html();
	}
	if($(sub).parent().length!=0)
		sub = $("#titleName").html();
	else
		if(id!="add"&&id!="del"&&id!="addsub"&&id!="editsub"&&id!="delsub"&&id!="addsubCatePage")currentSubPage.sub = undefined;
	if(currentSubPage.pointPosId!=""&&currentSubPage.pointPosId!=undefined){
		if(addTitle!=undefined){
			currentSubPage.categoryId = undefined;
		}
	}
	if((type=="next"||type=="previous")&&main!=undefined){
		posId = currentSubPage.posId;
		addTitle = currentSubPage.pointPos;
		sub = $("#subTitle").html();
	}
	setcurrentSubPage(id,addTitle,categoryId,posId,sub,subId);
	if($($("[name=filterPOS]")[0]).css("display")=="block");
		$("[name=filterPOS]").css("display","none");
	var parentCategory = "";
	if(data!=""&&data!=undefined){
		var objArray = new Array();
		objArray = JSON.parse(data);
		parentCategory = objArray.parentCategory;
	}
	var titlePos = "";
	if(categoryId!=""&&categoryId!=undefined||id=="addsub"||id=="editsub"||id=="delsub"){
		titlePos = "<span class='link' id='titlePOS' onclick=\"showPOSCategory('','','"+currentSubPage.pointPos+"','',"+currentSubPage.pointPosId+",undefined,undefined,'main')\" >"+currentSubPage.pointPos+"</span>/";
	}else
		titlePos = "";
	if(main!=""&&main!=undefined||parentCategory=="0")
		objectLink = new Array();
	if(currentSubPage.id!=undefined&&currentSubPage.id!=null&&currentSubPage.id!=""){
		if(currentSubPage.id!="add"&&currentSubPage.id!="del"&&currentSubPage.id!="addsub"&&currentSubPage.id!="editsub"
			&&currentSubPage.id!="delsub"&&currentSubPage.id!="addsubCatePage")
			id = currentSubPage.id;
	}
	if(currentSubPage.addTitle!=undefined&&currentSubPage.addTitle!=null&&currentSubPage.addTitle!=""){
			addTitle = currentSubPage.addTitle;
	}
	if(currentSubPage.categoryId!=undefined&&currentSubPage.categoryId!=null&&currentSubPage.categoryId!="")
		categoryId = currentSubPage.categoryId;
	if(currentSubPage.posId!=undefined&&currentSubPage.posId!=null&&currentSubPage.posId!="")
		posId = currentSubPage.posId;
	if(currentSubPage.sub!=undefined&&currentSubPage.sub!=null&&currentSubPage.sub!=""){
		if((id=="addsub"||id=="editsub"||id=="delsub")&&sub==undefined){
			sub = $("#subTitle").html();
		}else if(type!="next"&&type!="previous"){
			sub = currentSubPage.sub;
		}
	}
	if(currentSubPage.subId!=undefined&&currentSubPage.subId!=null&&currentSubPage.subId!="")
		subId = currentSubPage.subId;
	if(addTitle!=""&&addTitle!=undefined&&sub==undefined){
		var objectdataParentId = {idcategory:categoryId,posId:posId};
		var obj = objectdataParentId;
		for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
		var re2 = /"/gi;
		var sendData = JSON.stringify(obj).replace(re2, "'");
		if(main!=""&&main!=undefined||(parentCategory=="0"||parentCategory==undefined||id=="addsubCatePage")&&id!="delsub"&&id!="addsub"&&id!="editsub"){
			currentSubPage.titleForSubProduct = undefined; // resert for subProduct
			currentSubPage.idForSubProduct = undefined;
			currentSubPage.idPosForSubProduct = undefined;
			$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
					"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
					titlePos+
					'<span id="titleName" >'+addTitle+"</span></div>");
			$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategoryMain+"</div>");
			$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategoryMain+"<span id='listsize'></span></div>");
			$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
					+addCategoryMain+"\" type='button' onClick=\"gotoaddCategory($(this),"+sendData+");\"></span>");
			$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
					+delCategoryMain+"\" type='button' onClick='deleteCategories()'></span>");
		}else{
			currentSubPage.titleForSubProduct = addTitle;
			currentSubPage.idForSubProduct = categoryId;
			currentSubPage.idPosForSubProduct = posId;
			$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
					"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
					titlePos+
					'<span id="titleName" >'+addTitle+"</span></div>");
			$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategory+"</div>");
			$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategory+"<span id='listsize'></span></div>");
			$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
					+addCategory+"\" type='button' onClick=\"gotoaddSub($(this),"+sendData+");\"></span>");
			$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
					+delCategory+"\" type='button' onClick='deleteSubCategories()'></span>");
		}
		link = "";
	}
	if(sub!=undefined){
		var link = "";
		if(sub=="back"){
			objectLink = UniqueArray(objectLink);
			for(var i=0;i<objectLink.length;i++){
				if(objectLink[i].subId==categoryId){
					if(i>0){
						link = objectLink[i-1].subLink;
						delete objectLink[i];
						objectLink = objectLink.filter(function(a){return typeof a !== 'undefined';});
					}
				}
			}
			if(link!="")
				link = link+"/";
			var objectdataParentId = {idcategory:categoryId,posId:posId};
			var obj = objectdataParentId;
			for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
			var re2 = /"/gi;
			var sendData = JSON.stringify(obj).replace(re2, "'");
			currentSubPage.titleForSubProduct = addTitle;
			currentSubPage.idForSubProduct = categoryId;
			currentSubPage.idPosForSubProduct = posId;
			$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
					"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
					titlePos+
					link+'<span id="titleName" >'+addTitle+"</span></div>");
			$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategory+"</div>");
			$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategory+"<span id='listsize'></span></div>");
			$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
					+addCategory+"\" type='button' onClick=\"gotoaddSub($(this),"+sendData+");\"></span>");
			$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
					+delCategory+"\" type='button' onClick='deleteSubCategories()'></span>");
			link = "";
		}else if(sub=="back2"){
			titlePos = "<span class='link' id='titlePOS' category="+currentSubPage.pointPosId+" onclick=\""+$("#titlePOS").attr("onclick")+"\" >"
			+currentSubPage.pointPos+"</span>/";	
			link = "<span class='link' category="+categoryId+" id='subTitle' posId="+posId+" onclick=\""+$("#subTitle").attr("onclick")+"\" >"+$("#subTitle").html()+"</span>/";
			var objectdataParentId = {idcategory:categoryId,posId:posId};
			var obj = objectdataParentId;
			for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
			var re2 = /"/gi;
			var sendData = JSON.stringify(obj).replace(re2, "'");
			$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
					"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
					titlePos+
					link+'<span id="titleName" >'+addTitle+"</span></div>");
			$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategory+"</div>");
			$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategory+"<span id='listsize'></span></div>");
			$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
					+addCategory+"\" type='button' onClick=\"gotoaddSub($(this),"+sendData+");\"></span>");
			$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
					+delCategory+"\" type='button' onClick='deleteSubCategories()'></span>");
			link = "";
		}else{
			if(type!="next"&&type!="previous"){
				objectLink.push({subId:subId,subLink:"<span class='link' categoryid="+subId+" posid="+posId+" id='subTitle' onclick=\"showPOSCategory('','',this,\'"
					+subId+"\',\'"+posId+"\','back');\">"+sub+"</span>"});
				objectLink = UniqueArray(objectLink);
			}
			if(objectLink.length>1){
				if(id=="delsub"||id=="editsub"){
					//delete objectLink[objectLink.length-1];
					//	objectLink = objectLink = objectLink.filter(function(a){return typeof a !== 'undefined';});
					link = objectLink[objectLink.length-1].subLink;
				}else{
					link = objectLink[objectLink.length-1].subLink;
				}
			}else{
				link = objectLink[0].subLink;
			}
			link = link+"/";
			var objectdataParentId = {idcategory:categoryId,posId:posId};
			var obj = objectdataParentId;
			for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
			var re2 = /"/gi;
			var sendData = JSON.stringify(obj).replace(re2, "'");
			if(main!=""&&main!=undefined||parentCategory=="0"){
				currentSubPage.titleForSubProduct = undefined; // resert for subProduct
				currentSubPage.idForSubProduct = undefined;
				currentSubPage.idPosForSubProduct = undefined;
				$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
						"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
						titlePos+
						addTitle+"</div>");
				$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategoryMain+"</div>");
				$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategoryMain+"<span id='listsize'></span></div>");
				$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
						+addCategoryMain+"\" type='button' onClick=\"gotoaddCategory($(this),"+sendData+");\"></span>");
				$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
						+delCategoryMain+"\" type='button' onClick='deleteCategories()'></span>");
			}else{
				
				$("#titleLinkPOSCategory").replaceWith("<div class='breadcrumb' id='titleLinkPOSCategory' >" +
						"<a href='/adminconsole/POSManager/PointPos/PointPos.jsp'><span class='link'>"+titlePoint+"</span></a>/"+
						titlePos+
						link+'<span id="titleName" category='+categoryId+' >'+addTitle+"</span></div>");
				$("#suggestCategory").replaceWith("<div class='destitle' id='suggestCategory'>"+suggestCategory+"</div>");
				$("#listCategory").replaceWith("<div class='titlelist' id='listCategory'>"+listCategory+"<span id='listsize'></span></div>");
				$("#addCategory").replaceWith("<span id='addCategory'><input class='addbtn' name='addtaxes' value=\""
						+addCategory+"\" type='button' onClick=\"gotoaddSub($(this),"+sendData+");\"></span>");
				$("#delCategory").replaceWith("<span style='padding-left:10px' id='delCategory'><input class='deletebtn' name='deletetaxes' value=\""
						+delCategory+"\" type='button' onClick='deleteSubCategories()'></span>");
			}
			link = "";
		}
	}
	if(categoryId!=undefined&&categoryId!=""){
		if(id == null||id==""||id=="add"||id=="del"||id=="addsub"||id=="editsub"
			||id=="delsub"||id=="addsubCatePage"){
			if($("[name=maxEntries]").val() == null){
				var line = 10;
			}else{
				var line = $("[name=maxEntries]").val();
			}
			var pagePointer = 1;
		}else if(id == "next"){
			var line = $("[name=maxEntries]").val();
			var pagePointer = parseInt($("[name=selectpage]").val())+1;
		}else if(id == "previous"){
			var line = $("[name=maxEntries]").val();
			var pagePointer = parseInt($("[name=selectpage]").val())-1;
		}
		var objectDate = {"title":addTitle,"id":categoryId,"posId":posId};
		var link = "/POSCategoriesService";
		var data = { func: "getAllCategories",
					offset: (line*pagePointer)-line,
					line: line,
					pagePointer: pagePointer,
					categoryId:categoryId
			}
		simpleserviceWithArgs(link,data,showPOSSubCategoriesList,objectDate);
	}else{
		if(id == null||id==""||id=="add"||id=="del"||id=="addsub"||id=="editsub"
			||id=="delsub"||id=="addsubCatePage"){
			if($("[name=maxEntries]").val() == null){
				var line = 10;
			}else{
				var line = $("[name=maxEntries]").val();
			}
			if($("[name=selectpage]").val() == null){
				var pagePointer = 1; 
			}else{
				var pagePointer = $("[name=selectpage]").val(); 
			}
			if(main=="main")
				var pagePointer = 1;
		}else if(id == "next"){
			var line = $("[name=maxEntries]").val();
			var pagePointer = parseInt($("[name=selectpage]").val())+1;
		}else if(id == "previous"){
			var line = $("[name=maxEntries]").val();
			var pagePointer = parseInt($("[name=selectpage]").val())-1;		
		}
		var link = "/POSCategoriesService";
		var data = { func: "getAllCategories",
					offset: (line*pagePointer)-line,
					line: line,
					pagePointer: pagePointer,
					posId: posId
						}
		simpleservice(link,data,showPOSCategoriesList);
	}
	parentCategory = "";
}

function showPOSCategoriesList(data){
	$("[id=showList]").load("/adminconsole/POSManager/POSCategories/POSCategories.jsp",function(responseText, textStatus, XMLHttpRequest) {
		$("#showList").replaceWith(responseText);
		var objArray2 = new Array();
		objArray2 = JSON.parse(data);
		var sumtext = "";
		var text = "";
		var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
		var line = objArray2[2];
		var pagePointer = objArray2[1];
		var objArray = objArray2[3].data;
		$("#listsize").html("("+objArray2[0]+")");
		var lineNumber = (line * pagePointer) - (line-1);
		var description = "";
		var product = "";
		var countSubCat = 0;
		for(var i = 0;i< objArray.length+1;i++){
				if(objArray[i]!=undefined){
					if(objArray[i].subCategoryString!=undefined){
						countSubCat = objArray[i].subCategoryString.length;
					}else{
						countSubCat = 0;
					}
					if(objArray[i].productIdString!=undefined){
						product = objArray[i].productIdString;
					}else{
						product = 0;
					}
				}
				
				if(i != objArray.length ){
					var objectdataParentId = {idcategory:objArray[i].id,posId:objArray[i].posId};
					var obj = objectdataParentId;
					for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
					var re2 = /"/gi;
					var sendData = JSON.stringify(obj).replace(re2, "'");
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
							"<td align=\"left\" name=\"titleName\" > "+objArray[i]['title']+" </td>"+
							"<td align=\"center\"><span class='link' onclick='showPOSCategory(\"\",\"\",this,\""+objArray[i]['id']+"\",\""+objArray[i].posId+"\");' >"+countSubCat+"Items </span>" +
							"<span class='link' onclick=\"gotoaddSubCatePage($(this),"+sendData+") \">/ Add new </span></td>"+
							"<td align=\"center\"  ><span class='link' onclick='showPOSProduct(\"\",\""+objArray[i].id+"\",this,\""+
							objArray[i].posId+"\")' >"+product+"Items</span>  /<span class='link' onclick='gotoaddProduct($(this),\""+
							objArray[i].id+"\",\"\",\""+objArray[i].posId+"\",\""+'addproduct'+"\");' > Add new </span></td>"+
							"<td align=\"center\">"+objArray[i]['posTitle']+" </td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"EditPOSUnit($(this),{id:"+objArray[i]['id']+",taxId:'"+objArray[i].tax+"'})\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusCategories("+objArray[i]['id']+") \"> " +
							"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showPOSCategory()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showPOSCategory()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
		$("#showList tr").not($("#showList tr").first()).remove();
		$("#showList").append(sumtext);
		$("#pageAmount").text(pageAmount);
		if(pagePointer+1 <= pageAmount){
			$("#next").attr("onclick","showPOSCategory('',id,'','','','','','main','next')");
			$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
		}
		if(pagePointer > 1){
			$("#previous").attr("onclick","showPOSCategory('',id,'','','','','','main','previous')");
			$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
		}
		$("[name=maxEntries]").val(line);
		for ( var i = 0; i < pageAmount; i++) {
			$("[name=selectpage]").append(
					$("<option>").val((i+1)).html((i+1)));
			}
		$("[name=selectpage]").val(pagePointer);
		closeWaitingDialog();
	});
}

function showPOSSubCategoriesList(data,data2){
	$("[id=showList]").load("/adminconsole/POSManager/POSCategories/POSCategories.jsp",function(responseText, textStatus, XMLHttpRequest) {
		var objArray2 = new Array();
		objArray2 = JSON.parse(data);
		var sumtext = "";
		var text = "";
		var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
		var line = objArray2[2];
		var pagePointer = objArray2[1];
		var objArray = objArray2[3].data;
		$("#listsize").html("("+objArray2[0]+")");
		var lineNumber = (line * pagePointer) - (line-1);
		var description = "";
		var product = "";
		var countSubCat = 0;
		for(var i = 0;i< objArray.length+1;i++){
				if(objArray[i]!=undefined){
					if(objArray[i].subCategoryString!=undefined){
						countSubCat = objArray[i].subCategoryString.length;
					}else{
						countSubCat = 0;
					}
					if(objArray[i].productIdString!=undefined){
						product = objArray[i].productIdString;
					}else{
						product = 0;
					}
				}
				if(i != objArray.length ){
					var objectdataParentId = {idcategory:objArray[i].id,posId:objArray[i].posId};
					var obj = objectdataParentId;
					for (var k in obj){ if (obj.hasOwnProperty(k)){obj[k] = String(obj[k]);}} //object to string
					var re2 = /"/gi;
					var sendData = JSON.stringify(obj).replace(re2, "'");
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". <input type='hidden' id=\"textposid\" value=\""+data2.posId+"\"></td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
							"<td align=\"left\" name=\"titleName\" > "+objArray[i]['title']+" </td>"+
							"<td align=\"center\"><span class='link' onclick='showPOSCategory(\"\",\"\",this,\""+objArray[i]['id']+"\",\""+data2.posId+"\",this,\""+objArray[i].categoryId+"\");' >"+countSubCat+"Items </span> " +
							"<span class='link' onclick=\"gotoaddSub($(this),"+sendData+") \">/ Add new </span></td>"+
							"<td align=\"center\"  ><span class='link' onclick='showPOSProduct(\"\",\""+objArray[i].id+"\",this,\""+data2.posId+"\",\""+objArray[i].categoryId+"\")' >"+product+" Items </span>/" +
									"<span class='link' onclick='gotoaddProduct($(this),\""+objArray[i].id+"\",this,\""+data2.posId+"\",\""+'addproduct'+"\");'> Add new </span></td>"+
							"<td align=\"center\">"+objArray[i]['posTitle']+" </td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"EditPOSUnitSub($(this),{id:"+objArray[i]['id']+",taxId:'"+objArray[i].tax+"'})\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusCategories("+objArray[i]['id']+") \"> " +
							"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showPOSCategory()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"<input type='hidden' id=\"textposid\" value=\""+data2.posId+"\"></td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showPOSCategory()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
		$("#showList tr").not($("#showList tr").first()).remove();
		var sumtextEmpty = "";
		if(objArray==""){
			textEmpty = "<tr>"+
			"<td colspan=\"13\" style=\"padding:0;\">"+
			"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
			"<tbody>"+
				"<tr><td aling='left'>No Entries Found</td></tr>"+
				"<tr>"+
					"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
						"<select name=\"selectpage\" onchange=\"showPOSCategory()\">"+
						"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
					"<input type='hidden' id=\"textposid\" value=\""+data2.posId+"\"></td>"+
					"<td width=\"31%\" align=\"center\">"+foor_view+
						"<select name=\"maxEntries\" onchange=\"showPOSCategory(\"\",\""+data2.title+"\",\""+data2.id+"\")\">"+
							"<option style=\"color:#000000\"  value=\"10\">10</option>"+
							"<option style=\"color:#000000\"  value=\"20\">20</option>"+
							"<option style=\"color:#000000\"  value=\"50\">50</option>"+
							"<option style=\"color:#000000\"  value=\"100\">100</option>"+
							"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
						"</select>"+foor_rpp+" </td>" +
						"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
						" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
						"</tbody></table></td></tr>";
			sumtextEmpty += textEmpty;
			sumtext = sumtextEmpty;
		}
		$("#showList").append(sumtext);
		$("#pageAmount").text(pageAmount);
		if(pagePointer+1 <= pageAmount){
			$("#next").attr("onclick","showPOSCategory(\"\",id,\""+data2.title+"\",\""+data2.id+"\",undefined,undefined,undefined,undefined,\"next\")");
			$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
		}
		if(pagePointer > 1){
			$("#previous").attr("onclick","showPOSCategory(\"\",id,\""+data2.title+"\",\""+data2.id+"\",undefined,undefined,undefined,undefined,\"previous\")");
			$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
		}
		$("[name=maxEntries]").val(line);
		for ( var i = 0; i < pageAmount; i++) {
			$("[name=selectpage]").append(
					$("<option>").val((i+1)).html((i+1)));
			}
		$("[name=selectpage]").val(pagePointer);
		closeWaitingDialog();
	});
}

function EditCategories(id,dialog){
	if(id != null){
		var url = "/POSCategoriesService";
		var data = { func : "getPOSCategoriesById",
						id : id
		}
		simpleserviceWithArgs(url,data,editSuccessCate,dialog);
	}
}

function editSuccessCate(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	dialog.find("#title").val(objArray['title']);
	$("[name=pospoint] option[text=" + objArray.posId +"]").attr("selected","selected");
	$("[name=pospoint]").attr("disabled","disabled");
	dialog.find("#description").val(objArray['description']);
}

function changeStatusCategories(id){
	waitingDialog({});
	var url = "/POSCategoriesService";
	var data = { func : "editStatusPOScategoriesById",
			categoriesId : id
		}
	simpleservice(url,data,changeStatusCategoriesSuccess);
}

function changeStatusCategoriesSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteCategories(){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/POSCategoriesService";
	var data = { func : "delCategories",
			categoriesId : listCheckBox.substring(0,listCheckBox.length - 1)
		}
	simpleserviceWithArgs(url,data,showPOSCategory,'del');
}

function deleteSubCategories(){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/POSCategoriesService";
	var data = { func : "delSubCategories",
			categoriesId : listCheckBox.substring(0,listCheckBox.length - 1)
		}
	simpleserviceWithArgs(url,data,showPOSCategory,"delsub");
}


function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}